﻿using System;
using System.Linq;

namespace DAL
{
    public class SysDepartmentRepository : BaseRepository<SysDepartment>, IDisposable
    {
        /// <summary>
        /// 通过主键id，获取部门---查看详细，首次编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>部门</returns>
        public SysDepartment GetById(string id)
        {
            using (SysEntities db = new SysEntities())
            {
                return GetById(db, id);
            }                   
        }

        /// <summary>
        /// 通过主键id，获取部门---查看详细，首次编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>部门</returns>
        public SysDepartment GetById(SysEntities db, string id)
        { 
            return db.SysDepartment.SingleOrDefault(s => s.Id == id);
        }

        /// <summary>
        /// 确定删除一个对象，调用Save方法
        /// </summary>
        /// <param name="id">一条数据的主键</param>
        /// <returns></returns>    
        public int Delete(string id)
        {
            using (SysEntities db = new SysEntities())
            {
                SysDepartment deleteItem = GetById(db, id);
                if (deleteItem != null)
                {
                    db.SysDepartment.DeleteObject(deleteItem);
                }
                return Save(db);
            }
        }
 
        /// <summary>
        /// 删除对象集合
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="deleteCollection">主键的集合</param>
        public void Delete(SysEntities db, string[] deleteCollection)
        {
            //数据库设置级联关系，自动删除子表的内容   
            IQueryable<SysDepartment> collection = from f in db.SysDepartment
                    where deleteCollection.Contains(f.Id)
                    select f;
            foreach (var deleteItem in collection)
            {
                db.SysDepartment.DeleteObject(deleteItem);
            }
        }

        public void Dispose()
        {          
        }
    }
}

